package org.meituan0927;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Zane
 * @date 2021/9/27 9:45
 */
public class MeituanUtils {
    private static int count = 0;

    /**
     * 获取商家id
     *
     * @String url
     */
    public static String getMerchantId(String url) throws Exception {
        Document document = Jsoup.parse(getPage(url).asXml());
        Elements merchantUrls = document.select("dd.poi-list-item a.react");
        String newStr = "";
        if (null != merchantUrls) {
            String href = merchantUrls.get(0).attr("href");
            int index = href.indexOf("h5");
            if (index != -1) {
                href = href.replace("h5","");
            }
            //提取字符串里所有数字
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(href);
            newStr = m.replaceAll("").trim();
            return "/poi/"+newStr;
        }
        return null;
    }

    /**
     * 页面操作
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static void getMerchantCommentPage(String url) throws Exception {
        HtmlPage page = MeituanUtils.getPage(url);
        getMerchantComment(page);
        for (int i = 0; i < 2; i++) {
            page = ((DomElement) page.getByXPath("//*[@id=\"J-feedback-list\"]/dd[16]/div/a[2]").get(0)).click();
            getMerchantComment(page);
        }
    }

    /**
     * 获取评价
     *
     * @param page
     */
    public static void getMerchantComment(HtmlPage page) {
        Document document = Jsoup.parse(page.asXml());
        Elements commentInfo = document.select("div.feedbackCard");
        commentInfo.forEach(info -> {
            String userImg = info.select("div.user-pic div.imgbox").attr("data-src");
            String userName = info.select(".username").text();
            Integer commentStar = info.select(".stars img.star_full").size();
            String commentText = info.select(".comment").text();
            String commentTime = info.select(".time").text();
            Integer commentType = commentStar > 2 ? commentStar > 3 ? 1 : 2 : 3;
            Elements commentImgs = info.select(".pics .imgbox");
            System.out.println("用户头像:" + userImg);
            System.out.println("用户名:" + userName);
            System.out.println("评论类型:" + commentType);
            System.out.println("评论星级:" + commentStar);
            System.out.println("评论日期:" + commentTime);
            System.out.println("内容:" + commentText);
            System.out.println("评论img:");
            commentImgs.forEach(img -> System.out.println(img.attr("data-src")));
            System.out.println("第"+(count++)+"条评论爬取完成");
        });
    }

    /**
     * 获取page对象
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static HtmlPage getPage(String url) throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45);
        webClient.getOptions().setJavaScriptEnabled(true);//运行js脚本执行
        webClient.getCookieManager().setCookiesEnabled(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());//设置支持AJAX
        webClient.getOptions().setCssEnabled(false);//忽略css
        webClient.getOptions().setUseInsecureSSL(true);//ssl安全访问
        webClient.getOptions().setThrowExceptionOnScriptError(false);  //解析js出错时不抛异常
        webClient.getOptions().setTimeout(50000);  //超时时间  ms
        HtmlPage page = webClient.getPage(url);
        webClient.waitForBackgroundJavaScript(5000);   //等待js脚本执行完成
        return page;
    }

    public static int getCount() {
        return count;
    }
}
